package com.api.core.result;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author wsl
 * @version 1.0
 * @date 2020/4/9
 * @description 这是返回值格式类型
 */

@Data
@ToString
public class JsonResponse<T> implements Serializable {

    /**
     * 是否响应成功
     */
    private Boolean success = true;
    /**
     * 响应状态码
     */
    private Integer code;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 错误信息
     */
    private String message;

    /**
     * 返回数据封装
     *
     * @param message 状态信息
     * @param data    返回数据
     * @return
     */
    public static JsonResponse response(String message, Object data) {
        JsonResponse jsonResponse = new JsonResponse ();
        jsonResponse.setMessage (message);
        jsonResponse.setCode (ResultCodeEnum.SUCCESS.getCode ());
        jsonResponse.setData (data);
        jsonResponse.setSuccess (true);
        return jsonResponse;
    }

    /**
     * 返回数据封装
     *
     * @param data 返回数据
     * @return
     */
    public static JsonResponse success(Object data) {
        JsonResponse jsonResponse = new JsonResponse ();
        jsonResponse.setMessage (ResultCodeEnum.SUCCESS.getMessage ());
        jsonResponse.setCode (ResultCodeEnum.SUCCESS.getCode ());
        jsonResponse.setData (data);
        return jsonResponse;
    }

    /**
     * 返回错误信息
     *
     * @param message 错误信息
     * @return
     */
    public static JsonResponse error(String message) {
        JsonResponse jsonResponse = new JsonResponse ();
        jsonResponse.setCode (ResultCodeEnum.BASE_ERROR_CODE.getCode ());
        jsonResponse.setMessage (message);
        jsonResponse.setSuccess (false);
        return jsonResponse;
    }
}
